package com.yl.order.ui.component;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * create by cjx on 2024/5/15
 * desc:
 */
public class GridDividerItemDecoration extends RecyclerView.ItemDecoration {
    private int dividerSize;

    public GridDividerItemDecoration(int dividerWidth) {
        this.dividerSize = dividerWidth;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int spanCount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
        int position = parent.getChildAdapterPosition(view); // 获取view 在adapter中的位置。
        int column = position % spanCount; // view 所在的列

        outRect.left = column * dividerSize / spanCount; // column * (列间距 * (1f / 列数))
        outRect.right = dividerSize - (column + 1) * dividerSize / spanCount; // 列间距 - (column + 1) * (列间距 * (1f /列数))

        // 如果position > 行数，说明不是在第一行，则不指定行高，其他行的上间距为 top=mRowSpacing
        if (position >= spanCount) {
            outRect.top = dividerSize; // item top
        }
    }

    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

    }
}
